using System;
using System.Collections.Generic;
using System.Text;
using Csla;
using ENT.GP.Datos.MysqlHelper;
using System.Data;

namespace ENT.GP.Core.Autenticacion
{
    [Serializable]
    public class Autenticacion : CommandBase
    {
        string _Usuario;
        string _Clave;
        public DataSet DatosUsuario;
        #region Authorization Methods

        public static bool CanExecuteCommand()
        {
            // TODO: customize to check user role
            //return Csla.ApplicationContext.User.IsInRole("Role");
            return true;
        }

        #endregion

        #region Factory Methods

        public static Autenticacion Execute(string Usuario, string Clave)
        {
            if (!CanExecuteCommand())
                throw new System.Security.SecurityException("Not authorized to execute command");

            Autenticacion cmd = new Autenticacion(Usuario,Clave);
            cmd.BeforeServer();
            cmd = DataPortal.Execute<Autenticacion>(cmd);
            cmd.AfterServer();
            return cmd;
        }

        public Autenticacion(string Usuario, string Clave)
        {
            _Usuario = Usuario;
            _Clave = Clave;
        }

        #endregion

        #region Client-side Code

        // TODO: add your own fields and properties
        bool _result;

        public bool Result
        {
            get { return _result; }
        }

        private void BeforeServer()
        {
            // TODO: implement code to run on client
            // before server is called
        }

        private void AfterServer()
        {
            // TODO: implement code to run on client
            // after server is called
        }

        #endregion

        #region Server-side Code

        protected override void DataPortal_Execute()
        {
            DatosUsuario = new DataSet();
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[2];
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("USUARIO", _Usuario);
            parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("CLAVE", _Clave);            
            try
            {
                DatosUsuario = objhm.EjecutarStoreProcedure(parametros, "Verificar_Ingreso");
                if (DatosUsuario.Tables[0].Rows.Count>0)
                {
                    _result = true;
                }
                else
                    _result = false;
            }
            catch (Exception)
            {

                throw;
            }
            
        }

        #endregion
    }
}
